package com.sunsharing.wsdlcall.model;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ParseRequest {
	
	public static RequestModel parseResult(String context) throws Exception
	{
		Document doc = DocumentHelper.parseText(context);
        RequestModel req = new RequestModel();
		Element root = doc.getRootElement();
		String serviceId = root.element("ServiceId").getText();
		String SenderId = root.element("SenderId").getText();
		req.setServiceId(serviceId);
        req.setRequestId(SenderId);
		//req.setMethodName(root.element("method").attributeValue("name"));
		List<Element> pars = root.element("Inputs").elements("Input");
		for(Iterator<Element> iter = pars.iterator();iter.hasNext();)
		{
			RequestElement ele = null;
			Element ele2 = iter.next();
			String type = ele2.attributeValue("type");
            ele = new RequestElement();
            ele.setName(ele2.attributeValue("name"));
            req.getRequests().add(ele);
			if(type.equalsIgnoreCase("string"))
			{
				ele.setValue(ele2.getText());
			}
            if(type.equalsIgnoreCase("int"))
            {
                Integer v = new Integer(ele2.getText());
                ele.setValue(v);
            }
            if(type.equalsIgnoreCase("ArrayOfString"))
            {
                List list = ele2.selectNodes("Array/Item");
                Array<String> arr = new Array<String>();
                String[]result = new String[list.size()];
                arr.parse(list,result);
                ele.setValue(result);
            }

            if(type.equalsIgnoreCase("ArrayOfInt"))
            {
                List list = ele2.selectNodes("Array/Item");
                Array<Integer> arr = new Array<Integer>();
                Integer[]result = new Integer[list.size()];
                ele.setValue(result);
            }
            if(type.equalsIgnoreCase("ArrayOfLong"))
            {
                List list = ele2.selectNodes("Array/Item");
                Array<Long> arr = new Array<Long>();
                Long[]result = new Long[list.size()];
                ele.setValue(result);
            }
            if(type.equalsIgnoreCase("ArrayOfFloat"))
            {
                List list = ele2.selectNodes("Array/Item");
                Array<Float> arr = new Array<Float>();
                Float[]result = new Float[list.size()];
                ele.setValue(result);
            }
            if(type.equalsIgnoreCase("ArrayOfDouble"))
            {
                List list = ele2.selectNodes("Array/Item");
                Array<Double> arr = new Array<Double>();
                Double[]result = new Double[list.size()];
                ele.setValue(result);
            }
		}
		return req;
	}

}

class Array<T>
{
    public  Object parse(List list,T[] result) {


        if(list==null)
        {
            list = new ArrayList();
        }
        //String result[] = new String[list.size()];
        for(int i=0;i<list.size();i++)
        {
            Element ele = (Element)list.get(i);
            result[i] = (T)ele.getText();
        }
        return result;
    }
}
